package cn.caskm.souvenirshopsystem.service.impl;

import cn.caskm.souvenirshopsystem.domain.dto.PageDTO;
import cn.caskm.souvenirshopsystem.domain.po.Goods;
import cn.caskm.souvenirshopsystem.domain.po.GoodsComment;
import cn.caskm.souvenirshopsystem.domain.po.GoodsOrder;
import cn.caskm.souvenirshopsystem.domain.query.GoodsSearchQuery;
import cn.caskm.souvenirshopsystem.domain.vo.GoodsSearchVO;
import cn.caskm.souvenirshopsystem.service.GoodsCommentService;
import cn.caskm.souvenirshopsystem.service.GoodsOrderService;
import cn.caskm.souvenirshopsystem.service.GoodsService;
import cn.caskm.souvenirshopsystem.service.SearchService;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.*;
import java.util.stream.Collectors;

@Slf4j
@Service
@RequiredArgsConstructor
public class SearchServiceImpl implements SearchService {

    private final GoodsService goodsService;

    private final GoodsCommentService commentService;

    private final GoodsOrderService goodsOrderService;

    @Override
    public PageDTO<GoodsSearchVO> searchGoods(GoodsSearchQuery query) {
        // 获取参数
        Integer categoryId = query.getCategoryId();
        String goodsName = query.getGoodsName();
        BigDecimal minPrice = query.getMinPrice();
        BigDecimal maxPrice = query.getMaxPrice();

        // 查询数据
        Page<Goods> page = goodsService.lambdaQuery()
                .eq(categoryId != null, Goods::getCategoryId, categoryId)
                .like(StrUtil.isNotBlank(goodsName), Goods::getGoodsName, goodsName)
                .ge(minPrice != null && maxPrice == null, Goods::getGoodsPrice, minPrice)
                .le(maxPrice != null && minPrice == null, Goods::getGoodsPrice, maxPrice)
                .between(minPrice != null && maxPrice != null, Goods::getGoodsPrice, minPrice, maxPrice)
                .orderByDesc(Goods::getCreateTime)
                .page(new Page<>(query.getPageNum(), query.getPageSize()));
        List<Goods> records = page.getRecords();
        if (CollUtil.isEmpty(records)) {
            return PageDTO.empty();
        }

        Set<Integer> goodsIds = records.stream().map(Goods::getGoodsId).collect(Collectors.toSet());
        // 统计评论信息
        List<GoodsComment> comments = commentService.lambdaQuery().in(GoodsComment::getGoodsId, goodsIds).list();
        Map<Integer, Long> commentCountMap = Collections.emptyMap();
        if (CollUtil.isNotEmpty(comments)) {
            commentCountMap = comments.stream().collect(Collectors.groupingBy(GoodsComment::getGoodsId, Collectors.counting()));
        }

        // 统计下单信息
        List<GoodsOrder> orders = goodsOrderService.lambdaQuery()
                .eq(GoodsOrder::getOrderStatus, 3)
                .in(GoodsOrder::getGoodsId, goodsIds)
                .list();
        Map<Integer, Long> orderCountMap = Collections.emptyMap();
        if (CollUtil.isNotEmpty(orders)) {
            orderCountMap = orders.stream().collect(Collectors.groupingBy(GoodsOrder::getGoodsId, Collectors.counting()));
        }

        // 组装vo
        List<GoodsSearchVO> result = new ArrayList<>(records.size());
        for (Goods goods : records) {
            GoodsSearchVO vo = new GoodsSearchVO();
            Integer goodsId = goods.getGoodsId();
            vo.setId(goodsId);
            vo.setGoodsName(goods.getGoodsName());
            vo.setStock(goods.getSellStatus());
            vo.setPrice(goods.getGoodsPrice());
            vo.setCommentNums(commentCountMap.getOrDefault(goodsId, 0L));
            vo.setPayNums(orderCountMap.getOrDefault(goodsId, 0L));
            vo.setPicture(goods.getImage());
            result.add(vo);
        }

        return PageDTO.of(result, page.getPages(), page.getTotal());
    }
}
